package com.lagou.article.controller;

import com.lagou.article.pojo.Article;
import com.lagou.article.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * controller 层
 *
 * @author zyt
 * @date 2020/6/21 19:21
 */
@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/list")
    public String list(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
                       Article article, Model model) {
        if (0 >= pageNum) {
            pageNum = 1;
        }
        if (0 >= pageSize) {
            pageNum = 5;
        }
        Page<Article> allByPage = articleService.findAllByPage(pageNum, pageSize);
        model.addAttribute("list", allByPage.getContent());
        model.addAttribute("total", allByPage.getTotalElements());
        model.addAttribute("pageNum", allByPage.getNumber() + 1);
        model.addAttribute("pageSize", allByPage.getSize());
        return "/client/index";
    }
}
